package com.study.chapter07;

import com.study.entity.Event;
import com.study.chapter05.source.ClickSource;
import com.study.entity.UrlViewCount;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.SlidingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: LiuQun
 * @Date: 2022/8/10 22:30
 */
public class KeyedProcessWindowTopNTest {
    public static void main(String[] args) throws Exception {
        //环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<Event> stream = env.addSource(new ClickSource());


        //定义水位线
        SingleOutputStreamOperator<Event> watermarkOpt = stream.assignTimestampsAndWatermarks(
                WatermarkStrategy.<Event>forMonotonousTimestamps()
                        .withTimestampAssigner((data, l) -> data.timestamp)
        );

        //设置窗口
        SingleOutputStreamOperator<UrlViewCount> urlStream = watermarkOpt.keyBy(data -> data.url) //根据url分组
                .window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(5)))
                .aggregate(new UrlViewCountAgg(), new UrlViewCountProcess());

        urlStream.keyBy(url -> url.windowEnd)
                .process(new TopNProcessResult(2))
                .print();

        env.execute();
    }

    public static class  TopNProcessResult extends KeyedProcessFunction<Long,UrlViewCount,String>{

        //定义一个属性n
        private Integer n;

        //定义列表状态
        private ListState<UrlViewCount> countListState;

        public TopNProcessResult(Integer n){
            this.n = n;
        }

        //在环境中获取状态
        @Override
        public void open(Configuration parameters) throws Exception {
           countListState = getRuntimeContext().getListState(
                   new ListStateDescriptor<UrlViewCount>("url-pojo-list",Types.POJO(UrlViewCount.class))
           );
        }

        @Override
        public void processElement(UrlViewCount value, Context ctx, Collector<String> out) throws Exception {
            //将数据保存到状态中
            countListState.add(value);
            //注册定时器：windEnd + 1ms
            ctx.timerService().registerEventTimeTimer((ctx.getCurrentKey() + 1));
        }

        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
            List<UrlViewCount> urlViewCountList = new ArrayList<>();
            //从状态中获取数据并添加到集合中
            countListState.get().forEach(urlViewCountList::add);
            //排序
            urlViewCountList.sort((o1,o2) -> {
                return o2.num.intValue() - o1.num.intValue();
            });

            StringBuilder result = new StringBuilder();
            result.append("========================================\n");
            //取出前两个结果，并输出
            for (int i = 0; i < 2; i++) {
                UrlViewCount temp = urlViewCountList.get(i);
                String info = "No." + (i + 1) +
                        " url：" + temp.url +
                        " num：" + temp.num +
                        " 窗口结束时间：" + new Timestamp(ctx.getCurrentKey()) + "\n";
                result.append(info);

            }
            out.collect(result.toString());
            result.append("========================================\n");
        }
    }

    /**
     * 自定义AggregateFunction
     */
    public static class UrlViewCountAgg implements AggregateFunction<Event,Long,Long> {

        @Override
        public Long createAccumulator() {
            //初始数量为0
            return 0L;
        }

        @Override
        public Long add(Event value, Long accumulator) {
            return accumulator + 1;
        }

        @Override
        public Long getResult(Long accumulator) {
            return accumulator;
        }

        @Override
        public Long merge(Long a, Long b) {
            return null;
        }
    }

    /**
     * 自定义ProcessWindowFunction
     */
    public static class  UrlViewCountProcess extends ProcessWindowFunction<Long, UrlViewCount,String, TimeWindow> {

        @Override
        public void process(String url, Context context, Iterable<Long> elements, Collector<UrlViewCount> out) throws Exception {
            long start = context.window().getStart();
            long end = context.window().getEnd();
            // 迭代器中只有一个元素，就是增量聚合函数的计算结果
            Long num = elements.iterator().next();
            out.collect(new UrlViewCount(url, num, start, end));
        }
    }
}
